// components/data-select/index.js
import {
  api
} from '../../js/util'; 
const componentBehavior = require('../../js/common/ComponentBehavior')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    mainColor:api.titleColor||"#e56b14",//主题色
    mode:['月','季','年'],
    checkedIndex:0,
    quarterList:['一季度','二季度','三季度','四季度'],
    quarterIndex:0,
    date:'请点击选择'
  },
  ready:function(){
    let current = new Date();
    let year = current.getFullYear();
    let month = current.getMonth()+1;
    if(month<10){
      month = '0'+month;
    }
    this.setData({
      date:year+'-'+month
    })
    let obj = {
      mode:'月',
      value:year+'-'+month
    }
    this.triggerEvent('dateSelect',obj)
  },
  behaviors: [componentBehavior],
  /**
   * 组件的方法列表
   */
  methods: {
    //季度选择
    bindQuarterChange:function(e){
      let {mode,checkedIndex,quarterList} = this.data; 
      let year = new Date().getFullYear();
      let value = year+'-0'+(parseInt(e.detail.value)+1);
      let obj = {
        mode:mode[checkedIndex],
        value:value
      }
      this.setData({
        quarterIndex: e.detail.value
      })
      this.triggerEvent('dateSelect', obj)
    },
    //日期选择
    bindDateChange:function(e){
      let {mode,checkedIndex} = this.data; 
      let obj = {
        mode:mode[checkedIndex],
        value:e.detail.value
      }
      this.setData({
        date: e.detail.value
      })
      this.triggerEvent('dateSelect',obj)
    },
    //模式切换
    bindModeChange:function(e){
      let {mode} = this.data;
      let current = new Date();
      let year = current.getFullYear();
      let month = current.getMonth()+1;
      if(month<10){
        month = '0'+month;
      }
      let index =  e.currentTarget.dataset.index;
      let defaultDate,quarterIndex;
      if(mode[index]=='月'){
        defaultDate = year+'-'+month;
      }else if(mode[index]=='年'){
        defaultDate = year;
      }else{
        let quarter;
        switch (month) {
          case '01':
          case '02':
          case '03':
            quarter = '01';
            quarterIndex = 0;
            break;
          case '04':
          case '05':
          case '06':
            quarter = '02';
            quarterIndex = 1;
            break;
          case '07':
          case '08':
          case '09':
            quarter = '03';
            quarterIndex = 2;
            break;
          default:
            quarter = '04';
            quarterIndex = 3;
            break;
        }
        defaultDate = year+'-'+quarter;
      }
      this.setData({
        checkedIndex:index,
        date:defaultDate,
        quarterIndex:quarterIndex
      },function(){
        let obj = {
          mode:mode[index],
          value:defaultDate
        }
        this.triggerEvent('dateSelect',obj)
      })
    }
  }
})
